#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <iomanip>
#include <climits>
#include <string.h>
#include <numeric>
#include <cmath>
#include <stdlib.h>
#include <vector>
#include <stack>
#include <set>
#define INF 1e7
#define MAXN 100010
#define maxn 1000010
#define Mod 1000007
#define N 1299800
using namespace std;
typedef long long LL;

int prime[N];
int vis[N];
void run()
{
    int k = 1;
    for (LL i = 2; i <= N; ++i)
        if (!vis[i])
        {
            prime[k++] = i;
            for (LL j = i * i; j < N; j += i)
                vis[j] = 1;
        }
    /*for (int i = 1; i < 1300; ++i)
        cout << prime[i] << " ";
    cout << endl;*/
}

int n;
int ans = 0;

int main()
{
    run();
    while (cin >> n, n)
    {
        ans = 2;
        if (vis[n] == 0)
            ans = 0;
        else
        {
            int tmp = n;
            while (--tmp)
                if (vis[tmp])
                    ans++;
                else
                    break;
            tmp = n;
            while (++tmp)
                if (vis[tmp])
                    ans++;
                else
                    break;
        }
        cout << ans << endl;
    }
    return 0;
}